<?php
 class ArticleCategories extends CI_Model{
    function __construct(){
        parent::__construct();
    }
    
     /** 
     * Insert data for config
     * @author tien.nguyen
     */
    public function getList(){
        $this->db->select('news_categories.*, c1.NewsCategoryName as ParentName, news.NewsId, 
            news.NewsTitle');
        $this->db->from('news_categories');
        $this->db->join('news_categories as c1', 'c1.NewsCategoryId = 
                news_categories.NewsCategoryParent', 'left');
        $this->db->join('news', 'news.NewsId = news_categories.NewsDefault', 'left');
        $this->db->order_by('news_categories.NewsOrderCategory ASC, news_categories.CreatedDate DESC');
        $query = $this->db->get();
        $res = $query->result_array();        
        return $res;
    }
    
     /**
     * Get product by product id
     * @author tien.nguyen
     */
    public function getArticleCategory($articleCategoryId){
        $this->db->select();
        $this->db->from('news_categories');
        $this->db->join('news', 'news.NewsId = news_categories.NewsDefault', 'left');
        $this->db->where('news_categories.NewsCategoryId = '.$articleCategoryId);
        $query = $this->db->get();
        return $query->row_array();
    }
    
    /**
     * edit function in config
     * @author tien.nguyen
     */
    public function edit($data, $articleCategoryId){
        $data = $this->_convertData($data);
        
        //if have is NewsCategoryParent
        if(!empty($data['NewsCategoryParent'])){
            $parent = $this->getArticleCategory($data['NewsCategoryParent']);
            $data['NewsCategoryLevel'] = $parent->NewsCategoryLevel  + 1;
            $data['NewsOrderCategory'] = $this->getMaxOrderInCategory($data['NewsCategoryParent']);   
        }
        
        return $this->db->update('news_categories', $data, array('NewsCategoryId' => $articleCategoryId));
    }
    
    /**
     * Add customer
     * @author tien.nguyen 
     */
    public function add($data){
        $data = $this->_convertData($data);
        $data['CreatedDate'] = date('Y-m-d H;m:s');
        
        if(!empty($data['NewsCategoryParent'])){
            $parent = $this->getArticleCategory($data['NewsCategoryParent']);
            $data['NewsCategoryLevel'] = $parent['NewsCategoryLevel']  + 1;
            $data['NewsOrderCategory'] = $this->getMaxOrderInCategory($data['NewsCategoryParent']);
        }
        
        return $this->db->insert('news_categories', $data);
    }
    
    /**
     * Get max order 
     * @author tien.nguyen
     */     
    public function getMaxOrderInCategory($categoryId = null){
        $this->db->select('MAX(NewsOrderCategory) as Max');
        $this->db->from('news_categories');
        
        if($categoryId){
            $this->db->where('news_categories.NewsCategoryId = '.$categoryId);
        }    
        
        $query = $this->db->get();
        $level = $query->row()->Max + 1;
        
        //check order if have order please. update 
        $cate = $this->db->get_where('news_categories', array('NewsOrderCategory' => $level));
        if($cate){
            //update all level
            $query = "UPDATE  news_categories SET news_categories.NewsOrderCategory =  news_categories.NewsOrderCategory + 1
                WHERE news_categories.NewsOrderCategory >= ".$level;
            $this->db->query($query);
        }
        
        return $level;
    }
    
    /**
     * Get options
     * @author tien.nguyen
     */
    public function getOptions($maxLevel = null){
        $this->db->select('news_categories.NewsCategoryId, 
            news_categories.NewsCategoryName,
            news_categories.NewsCategoryLevel');
        $this->db->from('news_categories');
        $this->db->order_by('news_categories.NewsOrderCategory');
        
        //if exist max value level, the system will add where 
        // <= current level
        if($maxLevel){
            //$this->db->where('news_categories.NewsCategoryLevel <= ', $maxLevel);
        }
        
        $query = $this->db->get();
        $list = $query->result();
        //echo $this->db->last_query(); die;
        $res = array('' => '');
        foreach($list as $item){
            $res[$item->NewsCategoryId] = show_space($item->NewsCategoryLevel).' '.$item->NewsCategoryName;
        }        
        return $res;
    }        
    
    /**
     * Delete customer
     * @author tien.nguyen
     */
    public function delete($articleCategoryId){
        return $this->db->delete('news_categories', array('NewsCategoryId' => $articleCategoryId));
    }
    
     /**
     * Convert data array to array of product
     * @author tien.nguyen
     */
    private function _convertData($data){
        $res = array();
        $arr = array('NewsCategoryId', 'NewsCategoryName', 'NewsCategoryParent', 'NewsCategoryCode', 
            'NewsCategoryLevel', 'CreatedDate', 'NewsOrderCategory');
        foreach($arr as $item){
            if(isset($data[$item])){
                $res[$item] = $data[$item];
            }
        }
        return $res;
    }
    
    /**
     * Set default for category id and newsid
     * @author tien.nguyen
     */
    public function unSetNewsDefault($categoryId, $newsId){
        $data['NewsDefault'] = $newsId;
        return $this->db->update('news_categories',
            array('NewsDefault' => null), 
            array('NewsCategoryId' => $categoryId));
    }
    
    /**
     * Set default for category id and newsid
     * @author tien.nguyen
     */
    public function setNewsDefault($categoryId, $newsId){
        $data['NewsDefault'] = $newsId;
        return $this->db->update('news_categories', $data, array('NewsCategoryId' => $categoryId));
    }
 }
?>